﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BlogDH
{
    public partial class Register : System.Web.UI.Page
    {
        protected string errorStr = string.Empty;
        protected string username = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.HttpMethod == "POST")
            {
                username = Request.Form["uname"];
                string pwd = Request.Form["pwd"];

                string reg = "[0-9a-zA-Z]+[0-9a-zA-Z\\.-]*\\.[a-zA-Z]{2,4}";
                if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(pwd) || username.Length > 50 || pwd.Length > 20 || !System.Text.RegularExpressions.Regex.IsMatch(username, reg))
                {
                    //Response.Write("<script>alert('注册信息有误，请重新填写！')</script>");
                    errorStr = "博客域名格式错误，请重新填写！";
                }
                else
                {
                    BLL.UserInfo bll = new BLL.UserInfo();
                    bool flag = bll.Exists(username);

                    if (flag)
                    {
                        //Response.Write("<script>alert('')</script>");
                        errorStr = "域名已存在！";
                    }
                    else
                    {
                        BlogDH.Model.UserInfo userinfo = new Model.UserInfo()
                        {
                            UserName = username,
                            PWD = CommMethod.MD5Hash(pwd),
                            email = string.Empty,
                            phone = string.Empty,
                            lastLoginIP = GetUserIP(),
                            lastloginTime = System.DateTime.Now,
                            Sheng = 0,
                            Shi = 0,
                            Xian = 0,
                            UpdateTme = System.DateTime.Now,
                            CreateTime = System.DateTime.Now
                        };
                        flag = bll.Add(userinfo);

                        Model.BlogInfo bloginfo = new Model.BlogInfo()
                        {
                            BlogUrl = username,
                            BlogName = string.Empty,
                            BlogDesc = string.Empty,
                            BlogTags = string.Empty,
                            ComeCount = 0,
                            GoCount = 0,
                            IsShow = 1,
                            IsWWW = 1,
                            CreateYear = System.DateTime.Now.Year,
                            CreateMouth = System.DateTime.Now.Month,
                            Sheng = 0,
                            Shi = 0,
                            Xian = 0,
                            Userid = bll.GetMaxId(),
                            State = 1,
                            UpdateTime = System.DateTime.Now,
                            CreateTime = System.DateTime.Now
                        };

                        flag = new BLL.BlogInfo().Add(bloginfo);

                        if (flag)
                        {
                            CommMethod.SetCookieValue("u", username, System.DateTime.Now.AddDays(7));
                            Response.Write("<script>alert('注册成功，请登录后继续完善博客信息！');document.location='login.aspx'</script>");
                        }
                        else
                        {
                            //Response.Write("<script>alert('数据库写入错误，请重试，如有疑问请联系站长！')</script>");
                            errorStr = "'数据库写入错误，请重试！";
                        }
                    }
                }
            }
        }

        private string GetUserIP()
        {
            string ip = string.Empty;
            if (Context.Request.ServerVariables["HTTP_VIA"] != null)
            {
                ip = Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
            }
            else
            {
                ip = Context.Request.ServerVariables["REMOTE_ADDR"].ToString();
            }
            return ip;
        }
    }
}